class AITrainingSystem:
    """综合AI陪练系统"""

    def __init__(self):
        self.partners = {}
        self.current_user = None
        self.training_sessions = []

    def register_partner(self, partner_type, partner):
        """注册陪练类型"""
        self.partners[partner_type] = partner

    def start_training_session(self, partner_type, user_id):
        """开始训练会话"""
        if partner_type not in self.partners:
            raise ValueError(f"未找到 {partner_type} 类型的陪练")

        partner = self.partners[partner_type]
        self.current_user = user_id

        session = {
            "user_id": user_id,
            "partner_type": partner_type,
            "start_time": time.time(),
            "exercises": [],
            "performance": []
        }
        self.training_sessions.append(session)

        return partner, session

    def run_training_loop(self, partner_type, user_id, num_exercises=5):
        """运行训练循环"""
        partner, session = self.start_training_session(partner_type, user_id)

        print(f"开始 {partner_type} 训练，难度: {partner.difficulty}")
        print("=" * 50)

        total_score = 0

        for i in range(num_exercises):
            print(f"\n练习 {i + 1}/{num_exercises}:")

            # 生成练习
            exercise = partner.generate_exercise()
            print(f"题目: {exercise['question'] if 'question' in exercise else exercise['description']}")

            # 获取用户输入
            user_response = input("你的回答: ")

            # 评估回答
            correct_answer = exercise.get('correct_answer') or exercise.get('expected_response')
            evaluation = partner.evaluate_response(user_response, correct_answer)

            # 提供反馈
            feedback = partner.provide_feedback(user_response, evaluation)
            print(f"反馈: {feedback}")

            # 记录会话
            partner.record_session(partner_type, user_response, evaluation)
            session["exercises"].append({
                "exercise": exercise,
                "user_response": user_response,
                "evaluation": evaluation
            })

            total_score += evaluation.get('score', 0)

            # 调整难度
            if i % 2 == 0:  # 每2个练习调整一次难度
                performance = total_score / (i + 1)
                partner.adjust_difficulty(performance)
                print(f"难度已调整为: {partner.difficulty}")

        # 最终报告
        avg_score = total_score / num_exercises
        session["performance"].append(avg_score)

        print("\n" + "=" * 50)
        print(f"训练完成！平均得分: {avg_score:.2f}")
        print(f"最终难度: {partner.difficulty}")

        return session


# 使用示例
def main():
    # 创建训练系统
    training_system = AITrainingSystem()

    # 注册不同类型的陪练
    language_partner = LanguageTrainingPartner("英语陪练")
    programming_partner = ProgrammingTrainingPartner("编程陪练")

    training_system.register_partner("language", language_partner)
    training_system.register_partner("programming", programming_partner)

    # 开始训练
    try:
        # 语言训练
        training_system.run_training_loop("language", "user123", 3)

        # 编程训练
        training_system.run_training_loop("programming", "user123", 2)

    except KeyboardInterrupt:
        print("\n训练已中断")

    print(f"\n总共完成了 {len(training_system.training_sessions)} 个训练会话")


if __name__ == "__main__":
    main()